<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>Coverage Report</title>
    <link href="style.css" rel="stylesheet" type="text/css"/></head>
  <body onLoad="init()">
    <script>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */


function toArray(iter) {
    if (iter === null) {
        return null;
    }
    return Array.prototype.slice.call(iter);
}

function find(selector, elem) {
    if (!elem) {
        elem = document;
    }
    return elem.querySelector(selector);
}

function find_all(selector, elem) {
    if (!elem) {
        elem = document;
    }
    return toArray(elem.querySelectorAll(selector));
}

function sort_column(elem) {
    toggle_sort_states(elem);
    name = ((elem).closest('table')).id;
    console.log("Hi");
    console.log(name);
    var colIndex = toArray(elem.parentNode.childNodes).indexOf(elem);
    var key;
    if (elem.classList.contains('numeric')) {
        key = key_num;
    } else if (elem.classList.contains('result')) {
        key = key_result;
    } else {
        key = key_alpha;
    }
    sort_table(elem, key(colIndex), name);
}

function show_all_extras() {
    find_all('.col-result').forEach(show_extras);
}

function hide_all_extras() {
    find_all('.col-result').forEach(hide_extras);
}

function show_all_extras1() {
    find_all('.col-yname').forEach(show_extras);
}

function hide_all_extras1() {
    find_all('.col-yname').forEach(hide_extras);
}

function show_extras(colresult_elem) {
    var extras = colresult_elem.parentNode.nextElementSibling;
    var expandcollapse = colresult_elem.firstElementChild;
    extras.classList.remove("collapsed");
    expandcollapse.classList.remove("expander");
    expandcollapse.classList.add("collapser");
}

function hide_extras(colresult_elem) {
    var extras = colresult_elem.parentNode.nextElementSibling;
    var expandcollapse = colresult_elem.firstElementChild;
    extras.classList.add("collapsed");
    expandcollapse.classList.remove("collapser");
    expandcollapse.classList.add("expander");
}

function add_collapse() {
    // Add links for show/hide all
    var resulttable = find('table#results-table');
    var showhideall = document.createElement("p");
    showhideall.innerHTML = '<a href="javascript:show_all_extras()">Show all details</a> / ' +
                            '<a href="javascript:hide_all_extras()">Hide all details</a>';
    resulttable.parentElement.insertBefore(showhideall, resulttable);

    // Add show/hide link to each result
    find_all('.col-result').forEach(function(elem) {
        var collapsed = get_query_parameter('collapsed') || 'Passed';
        var extras = elem.parentNode.nextElementSibling;
        var expandcollapse = document.createElement("span");
        if (collapsed.includes(elem.innerHTML)) {
            extras.classList.add("collapsed");
            expandcollapse.classList.add("expander");
        } else {
            expandcollapse.classList.add("collapser");
        }
        elem.appendChild(expandcollapse);

        elem.addEventListener("click", function(event) {
            if (event.currentTarget.parentNode.nextElementSibling.classList.contains("collapsed")) {
                show_extras(event.currentTarget);
            } else {
                hide_extras(event.currentTarget);
            }
        });
    })

    var resulttable = find('table#yaml-table');
    var showhideall = document.createElement("p");
    showhideall.innerHTML = '<a href="javascript:show_all_extras1()">Show all details</a> / ' +
                            '<a href="javascript:hide_all_extras1()">Hide all details</a>';
    resulttable.parentElement.insertBefore(showhideall, resulttable);

    // Add show/hide link to each result
    find_all('.col-yname').forEach(function(elem) {
        var collapsed = get_query_parameter('collapsed') || 'Passed';
        var extras = elem.parentNode.nextElementSibling;
        var expandcollapse = document.createElement("span");
        if (collapsed.includes(elem.innerHTML)) {
            extras.classList.add("collapsed");
            expandcollapse.classList.add("expander");
        } else {
            expandcollapse.classList.add("collapser");
        }
        elem.appendChild(expandcollapse);

        elem.addEventListener("click", function(event) {
            if (event.currentTarget.parentNode.nextElementSibling.classList.contains("collapsed")) {
                show_extras(event.currentTarget);
            } else {
                hide_extras(event.currentTarget);
            }
        });
    })

}

function get_query_parameter(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

function init () {
    reset_sort_headers();

    add_collapse();

    toggle_sort_states(find('.initial-sort'));

    find_all('.sortable').forEach(function(elem) {
        elem.addEventListener("click",
                              function(event) {
                                  sort_column(elem);
                              }, false)
    });
    hide_all_extras();
    hide_all_extras1();

};

function sort_table(clicked, key_func, tname) {
    var rows = find_all('.'+tname+'-row');
    var reversed = !clicked.classList.contains('asc');
    var sorted_rows = sort(rows, key_func, reversed);
    /* Whole table is removed here because browsers acts much slower
     * when appending existing elements.
     */
    var thead = document.getElementById(tname+"-head");
    document.getElementById(tname).remove();
    var parent = document.createElement("table");
    parent.id = tname;
    parent.appendChild(thead);
    sorted_rows.forEach(function(elem) {
        parent.appendChild(elem);
    });
    document.getElementsByTagName(tname+"-BODY")[0].appendChild(parent);
}

function sort(items, key_func, reversed) {
    var sort_array = items.map(function(item, i) {
        return [key_func(item), i];
    });
    var multiplier = reversed ? -1 : 1;

    sort_array.sort(function(a, b) {
        var key_a = a[0];
        var key_b = b[0];
        return multiplier * (key_a >= key_b ? 1 : -1);
    });

    return sort_array.map(function(item) {
        var index = item[1];
        return items[index];
    });
}

function key_alpha(col_index) {
    return function(elem) {
        return elem.childNodes[1].childNodes[col_index].firstChild.data.toLowerCase();
    };
}

function key_num(col_index) {
    return function(elem) {
        return parseFloat(elem.childNodes[1].childNodes[col_index].firstChild.data);
    };
}

function key_result(col_index) {
    return function(elem) {
        var strings = ['Passed','Failed'];
        return strings.indexOf(elem.childNodes[1].childNodes[col_index].firstChild.data);
    };
}

function reset_sort_headers() {
    find_all('.sort-icon').forEach(function(elem) {
        elem.parentNode.removeChild(elem);
    });
    find_all('.sortable').forEach(function(elem) {
        var icon = document.createElement("div");
        icon.className = "sort-icon";
        icon.textContent = "vvv";
        elem.insertBefore(icon, elem.firstChild);
        elem.classList.remove("desc", "active");
        elem.classList.add("asc", "inactive");
    });
}

function toggle_sort_states(elem) {
    //if active, toggle between asc and desc
    if (elem.classList.contains('active')) {
        elem.classList.toggle('asc');
        elem.classList.toggle('desc');
    }

    //if inactive, reset all other functions and add ascending active
    if (elem.classList.contains('inactive')) {
        reset_sort_headers();
        elem.classList.remove('inactive');
        elem.classList.add('active');
    }
}

function is_all_rows_hidden(value) {
  return value.hidden == false;
}

</script>
    <h1>{{ name }}</h1>
    <p>Report generated on {{ date }} by <a href="https://pypi.python.org/pypi/riscof">riscof</a> v{{ version }}</p>
    <h2>Environment</h2>
    <table id="environment">
      <tr>
        <td>Riscof Version</td>
        <td>{{ riscof_version }}</td></tr>
      <tr>
        <td>Riscv-arch-test Version/Commit Id</td>
        <td>{{ rvarch_version }}</td></tr>
      <tr>
        <td>ISA</td>
        <td>{{ isa }}</td></tr>
      <tr>
        <td>User Spec Version</td>
        <td>{{ usv }}</td></tr>
      <tr>
        <td>Privilege Spec Version</td>
        <td>{{ psv }}</td></tr>
     </table>
     <h2>Yaml</h2>
      <table id="yaml-table">
      <thead id="yaml-table-head">
      <tr>
          <th col="yname">Name</th>
      </tr>
      </thead>
      <tbody>
          <tr>
            <td class="col-yname">{{ isa_yaml }}</td></tr>
          <tr>
            <td class="extra" colspan="1">
              <div class="log">{{ isa_specs }}</br></div></td>
          </tr>
          </tbody>
          <tbody>
          <tr>
            <td class="col-yname">{{ platform_yaml }}</td></tr>
          <tr>
            <td class="extra" colspan="1">
              <div class="log">{{ platform_specs }}</br></div></td>
          </tr>
          </tbody></table>
    <p>Please visit <a href="https://riscv-config.readthedocs.io/en/latest/yaml-specs.html">YAML specifications</a> for more information. </p>
    
    <sizes-table-body>
    <h2>Test Stats</h2>
    <table id ="sizes-table">
      <thead id="sizes-table-head">
        <tr>
          <th class="sortable name initial-sort" col="tname">Test Name</th>
          <th class="sortable numeric initial-sort" col="tfprint">Mem Footprint (Bytes)</th>
          <th class="sortable numeric initial-sort" col="tcode">Code size (Bytes)</th>
          <th class="sortable numeric initial-sort" col="tdata">Data size (Bytes)</th>
          <th class="sortable numeric initial-sort" col="tsign">Sign size (Bytes)</th>
          <th col="tcov">Covergroups</th>
        </tr>
      </thead>
        {% for result1 in results1 %}
        <tbody class= "{{ result1.repclass }}  sizes-table-row" >
        <tr>
          <td class="col-tname">{{ result1.test_name }}</td>
          <td class="col-tfprint">{{ result1.test_size[0] }}</td>          
          <td class="col-tcode">{{ result1.test_size[1] }}</td>          
          <td class="col-tdata">{{ result1.test_size[2] }}</td>          
          <td class="col-tsign">{{ result1.test_size[3] }}</td>          
          <td class="col-tcov">{{ result1.test_groups }}</td>          
        </tr></tbody>
        {% endfor %}
      </table>

    </sizes-table-body>
    <results-table-body>
    <h2>Coverage Report ( Total Coverpoints: {{ coverpoints }} )</h2>
    <table id="results-table">
      <thead id="results-table-head">
        <tr>
          <th class="sortable name initial-sort" col="name">Coverage Label</th>
          <th col="result">(Covered-points)/(Total-points)</th>
          <th class="sortable numeric initial-sort" col="percentage">Percentage</th>
          </tr>
        </thead>
        {% for result in results %}
        <tbody class= "{{ result.repclass }}  results-table-row" >
        <tr>
          <td class="col-name">{{ result.name }}</td>
          <td class="col-result">{{ result.coverage }}</td>
          <td class="col-percentage">{{ result.percentage }}%
            <div class="meter">
              <span style="width: {{ result.percentage }}%"></span>
            </div>
          </td>          
        <tr>
          <td class="extra" colspan="2">
          <div class="log">{{ result.log }}</br></div></td>
        </tr></tbody>
        {% endfor %}
      </table>
        </results-table-body>
      </body></html>

